package com.meizi.codegenerate.generate.server.application.service.impl;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.meizi.codegenerate.constant.AnnotationSettingValueConstant;
import com.meizi.codegenerate.constant.JavaClassNameConstant;
import com.meizi.codegenerate.constant.MethodNameConstant;
import com.meizi.codegenerate.constant.TableNameConstant;
import com.meizi.codegenerate.entity.ProjectPositionEntity;
import com.meizi.codegenerate.entity.TablesEntity;
import com.meizi.codegenerate.generate.server.application.service.assembler.AssemblerGenerate;
import com.meizi.codegenerate.packagegenerate.PackageGenerate;
import com.meizi.codegenerate.tool.page.PageData;
import com.meizi.codegenerate.utils.AnnotationUtils;
import com.meizi.codegenerate.utils.AttributeConvertUtils;
import com.meizi.utils.BeanConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pc
 * @date 2024/1/25 - 10:03 - 星期四
 * @project lambda
 * @package com.meizi.codegenerate.generate.server.application.service.impl
 * @by GGbomd猪猪侠
 * service 实现类
 */
@Slf4j
public class ServiceImplGenerate {


    /**
     * 生成serviceImpl 实现类
     * @param serviceName
     * @param tablesComment
     * @param projectPosition
     * @param paramQueryBody
     * @param detailByIdReq
     * @param reqName
     * @param deleteByIdReqName
     */
    public static void createIServiceImplGenerate(String tableName, String serviceName, TablesEntity tablesComment,
                                                  ProjectPositionEntity projectPosition, String paramQueryBody,
                                                  String detailByIdReq, String reqName, String deleteByIdReqName, String respBodyClassName) throws IOException {

        // 在相应的路径下创建对应的文件
        String serviceUrl = PackageGenerate.obtainApplicationServiceImplUrl(projectPosition);

        //获取service java类名
        String className = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, true)
                + JavaClassNameConstant.SERVICE_IMPL;
        // 获取完成的文件路径创建文件
        String fullPath = serviceUrl + className + TableNameConstant.JAVA;
        log.info("创建 application 下serviceImpl路径 fullPath = {}", fullPath);

        CompilationUnit compilationUnit = new CompilationUnit();

        // 添加package
        compilationUnit.setPackageDeclaration(PackageGenerate.applicationServiceImplPackage(projectPosition));

        compilationUnit.addImport("lombok.extern.slf4j.Slf4j");
        compilationUnit.addImport("org.springframework.stereotype.Service");
        compilationUnit.addImport("javax.annotation.Resource");

        // 添加接口文件
        ClassOrInterfaceDeclaration declaration = compilationUnit.addClass(className).setPublic(true);
        // 实现service
        declaration.addImplementedType(serviceName);
        //添加类级别注释
        declaration.setJavadocComment(tablesComment.getTableComment() + " 服务层接口实现");
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();

        // 添加类级别注解
        declaration.addAnnotation(Slf4j.class);

        String valueName = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, false)
                + JavaClassNameConstant.SERVICE_IMPL;
        map.put(AnnotationSettingValueConstant.SERVICE_VALUE, valueName);
        declaration.addAnnotation(AnnotationUtils.setAnnotation(Service.class, new NodeList<>(),
                map));
        clearMap(map);

        // 注入相关类
        String iRepositoryClassName = "I" + AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, true)
                + JavaClassNameConstant.REPOSITORY;
        String repositoryValueName = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, false)
                + JavaClassNameConstant.REPOSITORY;
        String repositoryClassName = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, true)
                + JavaClassNameConstant.REPOSITORY;
        map.put(AnnotationSettingValueConstant.RESOURCE_NAME, repositoryValueName);
        map.put(AnnotationSettingValueConstant.SERVICE_TYPE, repositoryClassName.getBytes());
        declaration.addField(iRepositoryClassName, repositoryValueName, Modifier.Keyword.PRIVATE)
                        .addAnnotation(AnnotationUtils.setAnnotation(Resource.class, new NodeList<>(), map));
        clearMap(map);

        String assemblerClassName = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, true)
                + JavaClassNameConstant.ASSEMBLER;
        String assemblerValueName = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, false)
                + JavaClassNameConstant.ASSEMBLER;
        map.put(AnnotationSettingValueConstant.RESOURCE_NAME, assemblerValueName);
        map.put(AnnotationSettingValueConstant.SERVICE_TYPE, assemblerClassName.getBytes());
        declaration.addField(assemblerClassName, assemblerValueName, Modifier.Keyword.PRIVATE)
                .addAnnotation(AnnotationUtils.setAnnotation(Resource.class, new NodeList<>(), map));
        clearMap(map);

        String respModelName = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, true);
        // getList
        MethodDeclaration getListMethod = methodGetList(MethodNameConstant.SERVICE_GET_LIST, respBodyClassName, respModelName ,  paramQueryBody, repositoryValueName ,assemblerValueName);
        declaration.addMember(getListMethod);


        // pagegetList
        MethodDeclaration getPageList = methodGetPageList(MethodNameConstant.SERVICE_GET_PAGE_LIST, respBodyClassName, respModelName ,  paramQueryBody, repositoryValueName ,assemblerValueName);
        declaration.addMember(getPageList);

        // getCount
        MethodDeclaration getCount = methodGetCount(MethodNameConstant.SERVICE_GET_COUNT,  paramQueryBody, repositoryValueName);
        declaration.addMember(getCount);

        // exists
        MethodDeclaration exists = methodExists(MethodNameConstant.SERVICE_EXISTS,  paramQueryBody, repositoryValueName);
        declaration.addMember(exists);

        // getDetailById
        MethodDeclaration getDetailById = methodGetDetailById(MethodNameConstant.SERVICE_GET_DETAIL_BYID, respBodyClassName, respModelName ,  detailByIdReq, repositoryValueName ,assemblerValueName);
        declaration.addMember(getDetailById);

        // getIdList
        MethodDeclaration getIdList = methodGetIdList(MethodNameConstant.SERVICE_GET_ID_LIST, paramQueryBody, repositoryValueName);
        declaration.addMember(getIdList);

        // create
        MethodDeclaration create = methodCreate(MethodNameConstant.SERVICE_CREATE, reqName, respModelName, repositoryValueName);
        declaration.addMember(create);

        // updateModel
        MethodDeclaration updateModel = methodUpdateModel(MethodNameConstant.SERVICE_UPDATE_MODEL, reqName, respModelName, repositoryValueName);
        declaration.addMember(updateModel);

        // saveModel
        MethodDeclaration saveModel = methodSaveModel(MethodNameConstant.SERVICE_SAVE_MODEL, reqName, respModelName, repositoryValueName);
        declaration.addMember(saveModel);

        // delete
        MethodDeclaration delete = methodDelete(MethodNameConstant.SERVICE_DELETE, paramQueryBody, repositoryValueName);
        declaration.addMember(delete);

        //deleteById
        MethodDeclaration deleteById = methodDeleteById(MethodNameConstant.SERVICE_DELETE_BYID, deleteByIdReqName, repositoryValueName);
        declaration.addMember(deleteById);

        log.info("生成serviceImpl接口 compilationUnit = {}", compilationUnit.toString());

        /**
         * 把字符串写入文件
         * 参数1：需要写入的文件，如果文件不存在，将自动创建。  参数2：需要写入的内容
         * 参数3：编码格式     参数4：是否为追加模式（ ture: 追加模式，把字符串追加到原内容后面）
         */
        FileUtils.writeStringToFile(new File(fullPath), compilationUnit.toString(), "UTF-8", false);

        //生成 汇编器 类
        AssemblerGenerate.createAssemblerGenerate(tableName, respModelName, respBodyClassName, tablesComment, projectPosition, assemblerClassName, assemblerValueName);

    }

    private static MethodDeclaration methodDeleteById(String deleteById, String reqName, String repositoryValueName) {

        String reqBody = "reqBody";
        String result = "result";
        String id = "id";
        // 添加返回值类型
        ClassOrInterfaceType resp = new ClassOrInterfaceType(null, Integer.class.getSimpleName());

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(resp);
        methodDeclaration.setName(deleteById);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, reqName), reqBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_DELETE_BY_ID));

        // 生成getId
        ClassOrInterfaceType respLong = new ClassOrInterfaceType(null, Long.class.getSimpleName());
        MethodCallExpr getIdExpr = new MethodCallExpr(
                new NameExpr(reqBody),
                "getId"
        );
        VariableDeclarationExpr idValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        respLong,
                        id,
                        getIdExpr
                )
        );


        // 生成调用仓储层调用存储层
        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_DELETE_BYID
        ).addArgument(id);
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resp,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成返回
        ReturnStmt returnStmt = new ReturnStmt(result);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(idValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodDelete(String delete, String paramQueryBody, String repositoryValueName) {

        String queryBody = "queryBody";
        String result = "result";
        // 添加返回值类型
        ClassOrInterfaceType resp = new ClassOrInterfaceType(Integer.class.getSimpleName());

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(resp);
        methodDeclaration.setName(delete);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, paramQueryBody), queryBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_DELETE));

        // 生成调用仓储层调用存储层
        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_DELETE
        ).addArgument(new NameExpr(queryBody));
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resp,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成return 返回
        ReturnStmt returnStmt = new ReturnStmt(result);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodSaveModel(String saveModel, String reqName, String respModelName, String repositoryValueName) {

        String model = "model";
        String result = "result";
        String reqBody = "reqBody";
        // 添加返回值类型
        ClassOrInterfaceType respIn = new ClassOrInterfaceType(null, Integer.class.getSimpleName());

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(respIn);
        methodDeclaration.setName(saveModel);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, reqName), reqBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_SAVE_MODEL));

        ClassOrInterfaceType respModelType = new ClassOrInterfaceType(null, respModelName);

        //  转换成model
        MethodCallExpr modelMethodCallExpr = new MethodCallExpr(
                new NameExpr(BeanConvertUtils.class.getSimpleName()),
                "convertTo"
        );
        modelMethodCallExpr.addArgument(new NameExpr(reqBody));
        modelMethodCallExpr.addArgument(new NameExpr(respModelName + "::new"));
        VariableDeclarationExpr modelValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        respModelType,
                        model,
                        modelMethodCallExpr
                )
        );

        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_SAVE_MODEL
        );
        resultMethodCallExpr.addArgument(new NameExpr(model));
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        respIn,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成返回
        ReturnStmt returnStmt = new ReturnStmt(result);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(modelValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodUpdateModel(String updateModel, String reqName, String respModelName, String repositoryValueName) {

        String model = "model";
        String result = "result";
        String reqBody = "reqBody";
        // 添加返回值类型
        ClassOrInterfaceType respIn = new ClassOrInterfaceType(null, Integer.class.getSimpleName());

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(respIn);
        methodDeclaration.setName(updateModel);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, reqName), reqBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_UPDATE_MODEL));

        ClassOrInterfaceType respModelType = new ClassOrInterfaceType(null, respModelName);

        //  转换成model
        MethodCallExpr modelMethodCallExpr = new MethodCallExpr(
                new NameExpr("BeanConvertUtils"),
                "convertTo"
        );
        modelMethodCallExpr.addArgument(new NameExpr(reqBody));
        modelMethodCallExpr.addArgument(new NameExpr(respModelName + "::new"));
        VariableDeclarationExpr modelValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        respModelType,
                        model,
                        modelMethodCallExpr
                )
        );

        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_UPDATE_MODEL
        );
        resultMethodCallExpr.addArgument(new NameExpr(model));
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        respIn,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成返回
        ReturnStmt returnStmt = new ReturnStmt(result);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(modelValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodCreate(String create, String reqName, String respModelName,
                                                  String repositoryValueName) {

        String model = "model";
        String result = "result";
        String reqBody = "reqBody";
        // 添加返回值类型
        ClassOrInterfaceType respIn = new ClassOrInterfaceType(null, Integer.class.getSimpleName());

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(respIn);
        methodDeclaration.setName(create);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, reqName), reqBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_CREATE));

        ClassOrInterfaceType respModelType = new ClassOrInterfaceType(null, respModelName);

        //  转换成model
        MethodCallExpr modelMethodCallExpr = new MethodCallExpr(
                new NameExpr("BeanConvertUtils"),
                "convertTo"
        );
        modelMethodCallExpr.addArgument(new NameExpr(reqBody));
        modelMethodCallExpr.addArgument(new NameExpr(respModelName + "::new"));
        VariableDeclarationExpr modelValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        respModelType,
                        model,
                        modelMethodCallExpr
                )
        );

        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_CREATE
        );
        resultMethodCallExpr.addArgument(new NameExpr(model));
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        respIn,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成返回
        ReturnStmt returnStmt = new ReturnStmt(result);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(modelValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodGetIdList(String getIdList, String paramQueryBody, String repositoryValueName) {

        String result = "result";
        String queryBody = "queryBody";
        // 添加返回值类型
        ClassOrInterfaceType respLong = new ClassOrInterfaceType(null, Long.class.getSimpleName());
        ClassOrInterfaceType respList = new ClassOrInterfaceType(null, List.class.getSimpleName());
        respList.setTypeArguments(respLong);

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(respList);
        methodDeclaration.setName(getIdList);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, paramQueryBody), queryBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_GET_ID_LIST));

        // 生成getId
        // 生成调用仓储层调用存储层
        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_GET_ID_LIST
        ).addArgument(queryBody);
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        respList,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成返回
        ReturnStmt returnStmt = new ReturnStmt(result);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodGetDetailById(String getDetailById, String respBodyClassName, String respModelName,
                                                         String detailByIdReq, String repositoryValueName, String assemblerValueName) {

        String reqBody = "reqBody";
        String result = "result";
        String resultRespBody = "resultRespBody";
        // 添加返回值类型
        ClassOrInterfaceType resp = new ClassOrInterfaceType(respModelName);

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(respBodyClassName);
        methodDeclaration.setName(getDetailById);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, detailByIdReq), reqBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_GET_DETAIL_BY_ID));

        // 生成getId
        MethodCallExpr getIdExpr = new MethodCallExpr(
                new NameExpr(reqBody),
                "getId"
        );
        // 生成调用仓储层调用存储层
        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_GET_DETAIL_BYID
        ).addArgument(getIdExpr);
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resp,
                        result,
                        resultMethodCallExpr
                )
        );

        // 返回resp
        ClassOrInterfaceType resultRespModelType = new ClassOrInterfaceType(null, respBodyClassName);
        VariableDeclarationExpr valuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resultRespModelType,
                        resultRespBody,
                        new MethodCallExpr(
                                new NameExpr(assemblerValueName),
                                MethodNameConstant.SERVICE_CONVERT_MODEL_TO_RESP_BODY
                        ).addArgument(new NameExpr(result))
                )
        );

        // 生成返回
        ReturnStmt returnStmt = new ReturnStmt(resultRespBody);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(valuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodExists(String exists, String paramQueryBody, String repositoryValueName) {

        String queryBody = "queryBody";
        String result = "result";
        // 添加返回值类型
        ClassOrInterfaceType resp = new ClassOrInterfaceType(Boolean.class.getSimpleName());

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(resp);
        methodDeclaration.setName(exists);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, paramQueryBody), queryBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_GET_EXISTS));

        // 生成调用仓储层调用存储层
        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_EXISTS
        ).addArgument(new NameExpr(queryBody));
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resp,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成return 返回
        ReturnStmt returnStmt = new ReturnStmt(result);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodGetCount(String getCount, String paramQueryBody,
                                                    String repositoryValueName) {
        String queryBody = "queryBody";
        String result = "result";
        // 添加返回值类型
        ClassOrInterfaceType resp = new ClassOrInterfaceType(Integer.class.getSimpleName());

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(resp);
        methodDeclaration.setName(getCount);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, paramQueryBody), queryBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_GET_COUNT));

        // 生成调用仓储层调用存储层
        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_GET_COUNT
        ).addArgument(new NameExpr(queryBody));
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resp,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成return 返回
        ReturnStmt returnStmt = new ReturnStmt(result);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));
        return methodDeclaration;
    }

    private static MethodDeclaration methodGetPageList(String getPageList, String respBodyClassName, String respModelName, String paramQueryBody,
                                                       String repositoryValueName, String assemblerValueName) {

        String queryBody = "queryBody";
        String result = "result";
        String pageList = "pageList";
        String resultRespBodyList = "resultRespBodyList";
        String resultRespBody = "resultRespBody";
        // 添加返回值类型
        ClassOrInterfaceType respBodyType = new ClassOrInterfaceType(respBodyClassName);
        ClassOrInterfaceType respList = new ClassOrInterfaceType(PageData.class.getSimpleName());
        respList.setTypeArguments(respBodyType);

        // 生成方法 getPageList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(respList);
        methodDeclaration.setName(getPageList);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, paramQueryBody), queryBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_GET_PAGE_LIST));

        // 生成调用仓储层调用存储层
        ClassOrInterfaceType resultModelType = new ClassOrInterfaceType(null, respModelName);
        ClassOrInterfaceType resultPageDataType = new ClassOrInterfaceType(null, PageData.class.getSimpleName());
        resultPageDataType.setTypeArguments(resultModelType);
        MethodCallExpr resultMethodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_GET_PAGE_LIST
        );
        resultMethodCallExpr.addArgument(new NameExpr(queryBody));
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resultPageDataType,
                        result,
                        resultMethodCallExpr
                )
        );

        // 生成获取pageList getList 不传参
        ClassOrInterfaceType resultTypeList = new ClassOrInterfaceType(null, List.class.getSimpleName());
        resultTypeList.setTypeArguments(resultModelType);
        VariableDeclarationExpr pageListValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resultTypeList,
                        pageList,
                        new MethodCallExpr(
                                new NameExpr(result),
                                "getList"
                        )
                )
        );



        // 返回resp
        ClassOrInterfaceType resultRespModelType = new ClassOrInterfaceType(null, respBodyClassName);
        ClassOrInterfaceType resultRespListType = new ClassOrInterfaceType(null, List.class.getSimpleName());
        resultRespListType.setTypeArguments(resultRespModelType);
        VariableDeclarationExpr valuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resultRespListType,
                        resultRespBodyList,
                        new MethodCallExpr(
                                new NameExpr(assemblerValueName),
                                MethodNameConstant.SERVICE_CONVERT_MODEL_LIST_TO_RESP_BODY_LIST
                        ).addArgument(new NameExpr(pageList))
                )
        );

        // 生成 pageDate
        ClassOrInterfaceType pageDataType = new ClassOrInterfaceType(null, PageData.class.getSimpleName()).setTypeArguments(respBodyType);
        ObjectCreationExpr initializationExpr = new ObjectCreationExpr(
                null,pageDataType , NodeList.nodeList());
        VariableDeclarator variableDeclarator = new VariableDeclarator(pageDataType,
                resultRespBody, initializationExpr);
        VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(variableDeclarator);

        // 生成 pageDate total:总记录数 list:列表数据
        //resultRespBody.setTotal(result.getTotal());
        //resultRespBody.setList(resultRespBodyList);
        MethodCallExpr setTotalExpr = new MethodCallExpr(
                new NameExpr(resultRespBody),
                "setTotal"
        );
        setTotalExpr.addArgument(new MethodCallExpr(
                new NameExpr(result),
                "getTotal"
        ));
        // 生成语句
        ExpressionStmt setTotalStmt = new ExpressionStmt(setTotalExpr);

        MethodCallExpr setListExpr = new MethodCallExpr(
                new NameExpr(resultRespBody),
                "setList"
        ).addArgument(resultRespBodyList);
        ExpressionStmt setListStmt = new ExpressionStmt(setListExpr);

        // 生成return 返回
        ReturnStmt returnStmt = new ReturnStmt(resultRespBody);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(pageListValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(valuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(variableDeclarationExpr));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(setTotalStmt));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(setListStmt));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));


        return methodDeclaration;

    }

    private static MethodDeclaration methodGetList(String getList, String respBodyClassName, String respModelName, String paramQueryBody,
                                                   String repositoryValueName, String assemblerValueName) {

        String queryBody = "queryBody";
        String result = "result";
        String resultRespBody = "resultRespBody";
        // 添加返回值类型
        ClassOrInterfaceType mapType = new ClassOrInterfaceType(respBodyClassName);
        ClassOrInterfaceType respList = new ClassOrInterfaceType(List.class.getSimpleName());
        respList.setTypeArguments(mapType);

        // 生成方法 getList
        MethodDeclaration methodDeclaration = new MethodDeclaration();
        methodDeclaration.setModifiers(Modifier.Keyword.PUBLIC);
        methodDeclaration.setType(respList);
        methodDeclaration.setName(getList);
        methodDeclaration.addParameter(new Parameter(new ClassOrInterfaceType(null, paramQueryBody), queryBody));

        // 生成注解
        methodDeclaration.addAnnotation(Override.class);
        // 将 valuesVar 添加到方法体的第一行
        methodDeclaration.setBody(new BlockStmt().addStatement(MethodNameConstant.LOG_GET_LIST));

        // 生成调用仓储层调用存储层
        ClassOrInterfaceType resultModelType = new ClassOrInterfaceType(null, respModelName);
        ClassOrInterfaceType resultListType = new ClassOrInterfaceType(null, List.class.getSimpleName());
        resultListType.setTypeArguments(resultModelType);
        MethodCallExpr methodCallExpr = new MethodCallExpr(
                new NameExpr(repositoryValueName),
                MethodNameConstant.SERVICE_GET_LIST
        );
        methodCallExpr.addArgument(new NameExpr(queryBody));
        VariableDeclarationExpr resultValuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resultListType,
                        result,
                        methodCallExpr
                )
        );

        // 返回resp
        ClassOrInterfaceType resultRespModelType = new ClassOrInterfaceType(null, respBodyClassName);
        ClassOrInterfaceType resultRespListType = new ClassOrInterfaceType(null, List.class.getSimpleName());
        resultRespListType.setTypeArguments(resultRespModelType);
        VariableDeclarationExpr valuesVar = new VariableDeclarationExpr(
                new VariableDeclarator(
                        resultRespListType,
                        resultRespBody,
                        new MethodCallExpr(
                                new NameExpr(assemblerValueName),
                                MethodNameConstant.SERVICE_CONVERT_MODEL_LIST_TO_RESP_BODY_LIST
                        ).addArgument(new NameExpr(result))
                )
        );

        // 生成返回
        ReturnStmt returnStmt = new ReturnStmt(resultRespBody);


        methodDeclaration.getBody().ifPresent(body -> body.addStatement(resultValuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(valuesVar));
        methodDeclaration.getBody().ifPresent(body -> body.addStatement(returnStmt));


        return methodDeclaration;
    }

    private static void clearMap(Map<String, Object> map) {
        map.clear();
    }
}
